package com.primeton.wh.mcenter.impl.message.controller;

import com.primeton.wh.mcenter.api.MessageClient;
import com.primeton.wh.mcenter.api.response.CodeMsg;
import com.primeton.wh.mcenter.api.response.Result;
import com.primeton.wh.mcenter.api.response.ResultGenerator;
import com.primeton.wh.mcenter.impl.client.CapUserClient;
import com.primeton.wh.mcenter.impl.client.CoframeUserLoginClient;
import com.primeton.wh.mcenter.impl.message.param.EmployeeDetailVO;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-12-11 14:26
 * @description:
 **/
@Controller
public class LoginController {
    @Autowired
    private CoframeUserLoginClient coframeClient;

    @Autowired
    private CapUserClient capUserClient;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private MessageClient messageClient;

    /**
     * 获得登陆的用户
     *
     * @return : Result
     * @author : YunTao.Li
     * @date : 2019/12/11 2019/12/11
     */
    @ResponseBody
    @RequestMapping("/login/user")
    @ApiOperation(value = "获得登陆的用户")
    public Result user(@RequestHeader HttpHeaders headers) {
        Map userMap = new HashMap();
        try {
            Map<String, Object> mapUser = this.getLoginUser();

            userMap.put("empCode", mapUser.get("empCode"));
            userMap.put("empName", mapUser.get("empName"));
            userMap.put("deptCode", mapUser.get("deptCode"));
            userMap.put("deptName", mapUser.get("deptName"));
//            userMap.put("empCode", "sysadmin1");
//            userMap.put("empName", "系统管理员");
//            userMap.put("deptCode", "10001");
//            userMap.put("deptName", "普元");
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }

        return Result.success(userMap);
    }

    private Map getLoginUser() {
        Map iamUser = new HashMap();

        // step1 : 调用coframe验证头信息里的用户权限
        Map loginUser = this.coframeClient.getLoginUser();
        if ((loginUser == null) || (StringUtils.isBlank((CharSequence) loginUser.get("name")))) {
            System.out.println("response of call coframe login return is null");
            return null;
        }
        String loginName = (String) loginUser.get("name");

       //  step2 : 根据用户登陆名获得用户详情(新);
        try {
            Result<EmployeeDetailVO> empDetailResult = capUserClient.getEmpDetail(null, loginName);
            EmployeeDetailVO empVO = empDetailResult.getData();
            if (empDetailResult.getCode() == CodeMsg.Success.getCode()) {
                String empCode = empVO.getEmpCode();
                String empName = empVO.getEmpName();


                iamUser.put("empCode", empCode);
                iamUser.put("empName", empName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return iamUser;
    }
}
